package cn.yunsenlin.formula.cloud.template;

import cn.yunsenlin.formula.cloud.auth.AuthRRH;
import cn.yunsenlin.formula.cloud.bean.auth.Template;
import cn.yunsenlin.formula.cloud.bean.template.TemplateShareParams;
import cn.yunsenlin.formula.cloud.interfaces.AuthService;
import cn.yunsenlin.formula.cloud.interfaces.TemplateService;
import cn.yunsenlin.formula.cloud.mould.ControllerMould;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.TbTemplate;
import cn.yunsenlin.formula.cloud.responsibility.ContinueAbleRRH;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/template")
@CrossOrigin
public class TemplateController {
    private final AuthRRH authRRH;
    private final TemplateService templateService;
    private final AuthService authService;

    public TemplateController(AuthRRH authRRH, TemplateService templateService, AuthService authService) {
        this.authRRH = authRRH;
        this.templateService = templateService;
        this.authService = authService;
    }

    @RequestMapping(path = "/", method = RequestMethod.PUT)
    public Template addTemplate(@RequestBody Template template, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.createTemplate(template, authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpObject(response, session);
    }

    @RequestMapping(path = "/", method = RequestMethod.POST)
    public List<TbTemplate> selectTemplate(HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.selectByUserId(authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpArray(response, session);
    }

    @RequestMapping(path = "/checkRepeatTemplateName/{name}", method = RequestMethod.POST)
    public boolean checkRepeatTemplateName(@PathVariable("name") String name,HttpServletResponse response, HttpSession session){
        return new ControllerMould(authRRH,new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.templateNameIsRepeat(name, authService.selectUserBySession(session).getId());
            }
        }) {
        }.startUpObject(response,session);
    }

    @RequestMapping(path = "/{groupId}", method = RequestMethod.POST)
    public List<Template> selectTemplateByGroupId(@PathVariable("groupId") Integer groupId, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.selectTemplateByGroupId(groupId);
            }
        }) {
        }.startUpArray(response, session);
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    public void removeTemplate(@PathVariable("id") Integer id, HttpServletResponse response, HttpSession session) {
        new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                templateService.removeTemplate(id);
                return null;
            }
        }) {
        }.startUpObject(response, session);
    }

    @RequestMapping(path = "/", method = RequestMethod.PATCH)
    public Template modifyTemplate(@RequestBody TbTemplate template, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.updateTemplateBaseInfo(template);
            }
        }) {
        }.startUpObject(response, session);
    }

    @RequestMapping(path = "/detail/{id}", method = RequestMethod.POST)
    public Template selectTemplateDetail(@PathVariable("id") Integer id, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.selectById(id);
            }
        }) {
        }.startUpObject(response, session);
    }

    @RequestMapping(path = "/shareTemplate/{id}", method = RequestMethod.POST)
    public Template shareTemplate(@PathVariable("id") Integer id, @RequestBody TemplateShareParams params, HttpServletResponse response, HttpSession session) {
        return new ControllerMould(authRRH, new ContinueAbleRRH() {
            @Override
            protected Object doHandler(HttpServletResponse response, Object object) {
                return templateService.shareTemplate(id,params);
            }
        }) {
        }.startUpObject(response, session);
    }
}
